#!/usr/bin/env python3
# Copyright 2024 Polar Bear Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, OpaqueFunction
from launch.substitutions import LaunchConfiguration, PathJoinSubstitution
from launch_ros.actions import Node, LifecycleNode
from launch_ros.substitutions import FindPackageShare
from ament_index_python.packages import get_package_share_directory


def generate_launch_description():
    """Generate launch description for drone serial interface."""
    
    # Package directory
    pkg_dir = get_package_share_directory('drone_serial_interface')
    
    # Launch arguments
    namespace_arg = DeclareLaunchArgument(
        'namespace',
        default_value='',
        description='Namespace for the drone serial interface node'
    )
    
    serial_device_arg = DeclareLaunchArgument(
        'serial_device',
        default_value='/dev/ttyUSB0',
        description='Serial device path (e.g., /dev/ttyUSB0 for Linux, COM3 for Windows)'
    )
    
    baudrate_arg = DeclareLaunchArgument(
        'baudrate',
        default_value='115200',
        description='Serial communication baudrate'
    )
    
    protocol_arg = DeclareLaunchArgument(
        'protocol_type',
        default_value='custom',
        description='Communication protocol type (custom, json, simple)'
    )
    
    safety_arg = DeclareLaunchArgument(
        'enable_safety_checks',
        default_value='true',
        description='Enable safety checks and velocity limits'
    )
    
    config_file_arg = DeclareLaunchArgument(
        'config_file',
        default_value=PathJoinSubstitution([
            FindPackageShare('drone_serial_interface'),
            'config', 'drone_serial_params.yaml'
        ]),
        description='Path to the configuration file'
    )
    
    # Drone serial interface lifecycle node
    drone_serial_node = LifecycleNode(
        package='drone_serial_interface',
        executable='drone_serial_interface_node',
        name='drone_serial_interface',
        namespace=LaunchConfiguration('namespace'),
        parameters=[
            LaunchConfiguration('config_file'),
            {
                'serial_device': LaunchConfiguration('serial_device'),
                'serial_baudrate': LaunchConfiguration('baudrate'),
                'protocol_type': LaunchConfiguration('protocol_type'),
                'enable_safety_checks': LaunchConfiguration('enable_safety_checks'),
            }
        ],
        output='screen',
        emulate_tty=True,
        arguments=['--ros-args', '--log-level', 'info']
    )
    
    return LaunchDescription([
        namespace_arg,
        serial_device_arg,
        baudrate_arg,
        protocol_arg,
        safety_arg,
        config_file_arg,
        
        drone_serial_node,
    ])